<?php

namespace App\Providers\Mobile;

use App\Contracts\Mobile\MobileMessage as MobileMessageContract;

/**
 * 发送手机短信
 * 服务商 luosimao.com
 * Class MobileCode
 * @package App\Services\Mobile
 */
class MobileMessage implements MobileMessageContract {


	/**
	 * ------------------------
	 *    0 => 'ok',
	 *  -10 => '验证信息失败',
	 *  -20 => '短信余额不足',
	 *  -30 => '短信内容为空',
	 *  -31 => '短信内容存在敏感词',
	 *  -32 => '短信内容缺少签名信息',
	 *  -40 => '错误的手机号',
	 *  -41 => '号码在黑名单中',
	 *  -50 => '请求发送IP不在白名单内',
	 * -------------------------
	 * @param        $mobile
	 * @param string $message
	 * @return mixed
	 * ---------------------------------
	 * return example
	 * {"error":0,"msg":"ok"}
	 * {"error":-10,"msg":"短信余额不足"}
	 */
	public function send($mobile, $message = '') {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, env('MOBILE_MESSAGE_API'));

		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);

		curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_USERPWD  , 'api:'.env('MOBILE_MESSAGE_API_KEY'));

		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, array('mobile' => $mobile,'message' => $message));
		$res = curl_exec( $ch );
		curl_close( $ch );

		return json_decode($res,true);

	}

}